package com.blog.video.util;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.vod.model.v20170321.GetPlayInfoRequest;
import com.aliyuncs.vod.model.v20170321.GetPlayInfoResponse;
import org.springframework.beans.factory.annotation.Value;

import java.util.List;

public class VideoUtil {

    //VOD API 的服务接入地址为：vod.{ApiRegion}.aliyuncs.com，{ApiRegion}请替换成对应API接入区域的标识
    private final static String VOD_DOMAIN = "http://vod.cn-shanghai.aliyuncs.com";

    public static DefaultAcsClient initVodClient(String accessKeyId, String accessKeySecret) throws ClientException {
        String regionId = "cn-shanghai";  // 点播服务接入区域
        DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
        DefaultAcsClient client = new DefaultAcsClient(profile);
        return client;
    }

    /*获取播放地址函数*/
    public static GetPlayInfoResponse getPlayInfo(DefaultAcsClient client, String VideoId) throws Exception {
        GetPlayInfoRequest request = new GetPlayInfoRequest();
        request.setVideoId(VideoId);
        return client.getAcsResponse(request);
    }

    @Value("${aliyun.accessKeyId}")
    private static String access_key_id;
    //账号AccessKey信息请填写(必选)
    @Value("${aliyun.accessKeySecret}")
    private static String access_key_secret;

    public static void main(String[] args) {
        try {
            DefaultAcsClient client = VideoUtil.initVodClient(access_key_id, access_key_secret);
            GetPlayInfoResponse response = new GetPlayInfoResponse();
            String playURL = "";

            response = VideoUtil.getPlayInfo(client, "4ad82587528b4580b6dff10d60c93448");
            List<GetPlayInfoResponse.PlayInfo> playInfoList = response.getPlayInfoList();
            //播放地址
            for (GetPlayInfoResponse.PlayInfo playInfo : playInfoList) {
                System.out.print("PlayInfo.PlayURL = " + playInfo.getPlayURL() + "\n");
                playURL = playInfo.getPlayURL();
            }
            //Base信息
            System.out.print("VideoBase.Title = " + response.getVideoBase().getTitle() + "\n");
        } catch (Exception e) {
            System.out.print("ErrorMessage = " + e.getLocalizedMessage());
        }
    }
}